//
//  Album.m
//  UI06__UIScrollView__相册
//
//  Created by dllo on 16/3/16.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Album.h"
#import "MyScorollView.h"

@interface Album () <UIScrollViewDelegate>
//声明两个子控件
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) MyScorollView *scroll;




@end





@implementation Album

#pragma mark - init  creating
-(instancetype)initWithFrame:(CGRect)frame withImages:(NSArray<UIImage *>*)images{
    self = [super initWithFrame:frame];
    if (self) {
        [self configWithFrame:frame images:images];
    }
    
    return self;
    
}

-(void)configWithFrame:(CGRect)frame images:(NSArray *)images{
    //创建对象  注意frame参数原点不是(0,0) 可用bounds属性代替
    self.scroll = [[MyScorollView alloc]initWithFrame:self.bounds withImages:images];
    self.scroll.contentOffset = CGPointMake(self.viewForLastBaselineLayout.bounds.size.width, 0);
    [self addSubview:_scroll];
   
    //指定代理人
    self.scroll.delegate = self;
   
    
    
    
    
    
    //创建pageControll对象
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size .height - 40, frame.size.width, 40)];
    [self addSubview:self.page];
    
    self.page.numberOfPages = images.count - 2;
    //self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
    //self.page.pageIndicatorTintColor = [UIColor blueColor];
    //添加事件
    [self.page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
    
    
    
    
}

-(void)handlePage:(UIPageControl *)page{
    //scroll滚动(contentOffset)  点击圆点  使之而动
    [self.scroll setContentOffset:CGPointMake(self.bounds.size.width * (page.currentPage + 1), 0) animated:YES];
    
    
    
    
    
    
}

#pragma mark - ScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
 
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
   //page跟随scroll滚动而改变
    if (self.scroll.contentOffset.x < self.frame.size.width) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width * self.page.numberOfPages, 0);
    }else if (self.scroll.contentOffset.x > self.frame.size.width * self.page.numberOfPages) {
        scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
    }
    self.page.currentPage = (scrollView.contentOffset.x - self.frame.size.width ) / self.frame.size.width;

    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
